/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Visual;

import Persistencia.exceptions.PreexistingEntityException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import Logica.*;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JDesktopPane;
import javax.swing.JList;

/**
 *
 * @author Alberto
 */
public class frmAltaEmpleado extends javax.swing.JInternalFrame {

    ControladoraVisual miVisual;
    JDesktopPane miEscritorio;
    DefaultListModel modeloEspecialidades = new DefaultListModel();
    DefaultComboBoxModel modeloCategorias = new DefaultComboBoxModel();

    /**
     * Creates new form AltaPersonal
     */
    public frmAltaEmpleado(ControladoraVisual miVisual, JDesktopPane miEscritorio) {
        initComponents();
        this.miVisual = miVisual;
        this.miEscritorio = miEscritorio;
        this.cmbCategorias.setModel(modeloCategorias);
        this.listEspecialidades.setModel(modeloEspecialidades);
        this.cargarCategorias();
        this.btnAsignarTaller.setEnabled(false);
    }

    public void cargarCategorias() {
        List<CategoriaEmp> categorias = miVisual.dameCategoriasEmp();
        for (CategoriaEmp miCategoria : categorias) {
            cmbCategorias.addItem(miCategoria);
        }
    }

    public List<Especialidad> crearListaEspecialidades() {
        List<Especialidad> especialidades = new LinkedList();
        for (int i = 0; this.listEspecialidades.getModel().getSize() > i; i++) {

            Especialidad miEspecialidad = (Especialidad) this.listEspecialidades.getModel().getElementAt(i);
            especialidades.add(miEspecialidad);

        }
        return especialidades;
    }

    public Calendar cargarFecha() {
        Calendar fechaNac = null;
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yy");
        String fechita = cmbFechas.getText();
        try {
            Date fecha = formatter.parse(fechita);
            fechaNac = Calendar.getInstance();
            fechaNac.setTime(fecha);
        } catch (ParseException ex) {
            Logger.getLogger(frmAltaEmpleado.class.getName()).log(Level.SEVERE, null, ex);
        }
        return fechaNac;
    }

    public void altaEmpleado() {
        int nroLegajo = Integer.valueOf(txtNroLegajo.getText());
        String nombre = txtNombre.getText();
        String apellido = txtApellido.getText();
        double dni = Double.valueOf(txtNroDoc.getText());
        Calendar fechaNac = this.cargarFecha();
        String domicilio = txtDomicilio.getText();
        long tel = Long.valueOf(txtTelefono.getText());
        CategoriaEmp cat = (CategoriaEmp) this.cmbCategorias.getSelectedItem();
        Operacion op = null;
        PedidoRepuesto ped = null;
        Taller unTaller = null;
        List<Especialidad> espec = this.crearListaEspecialidades();
        Set<Especialidad> especialidades = new HashSet(espec);


        try {
            miVisual.altaEmpleado(nroLegajo, cat, nombre, apellido, fechaNac, dni, domicilio, tel, especialidades);
        } catch (PreexistingEntityException ex) {
            Logger.getLogger(frmAltaEmpleado.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(frmAltaEmpleado.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnSalir = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txtNroDoc = new javax.swing.JTextField();
        txtApellido = new javax.swing.JTextField();
        txtNroLegajo = new javax.swing.JTextField();
        txtNombre = new javax.swing.JTextField();
        txtDomicilio = new javax.swing.JTextField();
        txtTelefono = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        btnAgregarEspecialidades = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        cmbCategorias = new javax.swing.JComboBox();
        btnAltaCategoria = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        btnAltaEmpleado = new javax.swing.JButton();
        cmbFechas = new datechooser.beans.DateChooserCombo();
        jScrollPane1 = new javax.swing.JScrollPane();
        listEspecialidades = new javax.swing.JList();
        btnAsignarTaller = new javax.swing.JButton();

        setTitle("Alta Personal");

        btnSalir.setText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        jLabel1.setText("Nro de Legajo");

        jLabel2.setText("Nombre");

        jLabel3.setText("Apellido");

        jLabel4.setText("Nro de Documento");

        jLabel5.setText("Fecha Nacimiento");

        jLabel6.setText("Domicilio");

        jLabel7.setText("Telefono");

        txtNroLegajo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNroLegajoActionPerformed(evt);
            }
        });

        jLabel8.setText("Especialidades");

        btnAgregarEspecialidades.setText("Agregar Especialidades");
        btnAgregarEspecialidades.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarEspecialidadesActionPerformed(evt);
            }
        });

        jLabel9.setText("Categoría");

        btnAltaCategoria.setText("Nueva Categoría");
        btnAltaCategoria.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAltaCategoriaActionPerformed(evt);
            }
        });

        btnAltaEmpleado.setText("Alta Empleado");
        btnAltaEmpleado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAltaEmpleadoActionPerformed(evt);
            }
        });

        listEspecialidades.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { " " };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(listEspecialidades);

        btnAsignarTaller.setText("Asignar Taller");
        btnAsignarTaller.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAsignarTallerActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtNroDoc, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)
                            .addComponent(txtApellido)
                            .addComponent(txtNroLegajo)
                            .addComponent(txtNombre))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel8)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jScrollPane1)
                                .addContainerGap())))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(btnAltaEmpleado)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnAsignarTaller)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnSalir))
                            .addComponent(jSeparator1)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel6)
                                    .addComponent(jLabel7))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(cmbFechas, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(72, 72, 72)
                                        .addComponent(btnAgregarEspecialidades, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(txtTelefono, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE)
                                            .addComponent(txtDomicilio))
                                        .addGap(18, 18, 18)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabel9)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(cmbCategorias, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                            .addGroup(layout.createSequentialGroup()
                                                .addGap(0, 0, Short.MAX_VALUE)
                                                .addComponent(btnAltaCategoria)))))))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(txtNroLegajo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(txtApellido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(txtNroDoc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel5)
                    .addComponent(cmbFechas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAgregarEspecialidades))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtDomicilio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmbCategorias, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAltaCategoria))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnSalir)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnAltaEmpleado, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnAsignarTaller)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnSalirActionPerformed

    private void btnAltaEmpleadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAltaEmpleadoActionPerformed
        this.altaEmpleado();
        this.btnAsignarTaller.setEnabled(true);


    }//GEN-LAST:event_btnAltaEmpleadoActionPerformed

    private void txtNroLegajoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNroLegajoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtNroLegajoActionPerformed

    private void btnAltaCategoriaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAltaCategoriaActionPerformed
        frmAltaCategoriaEmp unaCategoria = new frmAltaCategoriaEmp(miVisual, modeloCategorias);
        miEscritorio.add(unaCategoria);
        unaCategoria.setMaximizable(true);
        unaCategoria.setClosable(true);
        unaCategoria.setVisible(true);
    }//GEN-LAST:event_btnAltaCategoriaActionPerformed

    private void btnAgregarEspecialidadesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarEspecialidadesActionPerformed
        Especialidades especialidades = new Especialidades(miVisual, miEscritorio, modeloEspecialidades, listEspecialidades);
        miEscritorio.add(especialidades);
        especialidades.setMaximizable(true);
        especialidades.setClosable(true);
        especialidades.setVisible(true);
    }//GEN-LAST:event_btnAgregarEspecialidadesActionPerformed

    private void btnAsignarTallerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAsignarTallerActionPerformed
        double dni = Double.valueOf(txtNroDoc.getText());
        TallerEmpleado miTallerEmpleado = new TallerEmpleado(miVisual, dni);
        miEscritorio.add(miTallerEmpleado);
        miTallerEmpleado.setVisible(true);
    }//GEN-LAST:event_btnAsignarTallerActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregarEspecialidades;
    private javax.swing.JButton btnAltaCategoria;
    private javax.swing.JButton btnAltaEmpleado;
    private javax.swing.JButton btnAsignarTaller;
    private javax.swing.JButton btnSalir;
    private javax.swing.JComboBox cmbCategorias;
    private datechooser.beans.DateChooserCombo cmbFechas;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JList listEspecialidades;
    private javax.swing.JTextField txtApellido;
    private javax.swing.JTextField txtDomicilio;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JTextField txtNroDoc;
    private javax.swing.JTextField txtNroLegajo;
    private javax.swing.JTextField txtTelefono;
    // End of variables declaration//GEN-END:variables
}
